package dev.loong.boot3.component;

import dev.loong.boot3.pojo.Role;
import dev.loong.boot3.pojo.User;
import dev.loong.boot3.service.DocUserService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

//@Component
public class MongoTest {
    private final DocUserService docUserService;
    @Autowired
    public MongoTest(DocUserService docUserService) {
        this.docUserService = docUserService;
    }

    @PostConstruct
    public void test() {
        saveUser();
        findUsers();
    }

    private void saveUser() {
        for (var i=1L; i<=10L; i++) {
            var user = new User();
            user.setId(i);
            user.setUserName("user_name_" + i);
            user.setNote("user_note_" + i);

            var role1 = new Role();
            role1.setId(i);
            role1.setRoleName("role_name_" + i);
            role1.setNote("role_note_" + i);
            var role2 = new Role();
            role2.setId(i + 1);
            role2.setRoleName("role_name_" + (i + 1));
            role2.setNote("role_note_" + (i + 1));
            user.setRoles(List.of(role1, role2));
            docUserService.saveUser(user);
        }
    }

    private void findUsers() {
        var users = docUserService.findUser("user", "note", 3, 5);
        for (var user : users) {
            System.out.println("[id] = " + user.getId() + ",\t[user_name] = " + user.getUserName());
        }
    }
}
